var api = require('../../../utils/api.js')
var $ = require('../../../utils/util.js')
var regeneratorRuntime = require('../../../utils/runtime.js')
var app = getApp()
var Store = app.Store 

Page({
  data: {
    companyName:'',
    companyId:'',
    projectName:'',
    projectId:'',
    stationName:'',
    stationId:'',
    areaName:'',
    areaId:'',
    locakName:'',
    lockCode:'',
    lockId:'',
    lockListId:'',
    applyReason:'',
    applyStartTime: "",
    applyEndTime: "",
    dateNum:'',
    isShowTime: false, // 是否显示时间弹框
    isShowTime2: false, // 是否显示时间弹框
    type: '', // 类型
    urlName:''
  },
  onLoad: function (options) { 
    this.setData({dateNum:new Date().getTime(),lockId: Store.getState().main.lock_code})
    if('type' in options){
      this.setData({type:options.type,urlName:options.name})
    }
  },

  onShow(){
    var form = Store.getState().formInfor.form
    var key = form.key
    if (!form.selected) return
    switch (key) {
      case 'selectLockCompany': // 企业名称  
        this.setData({
          "companyName": form.selected.label,
          "companyId": form.selected.id,
        })
        break
      case 'selectLockProject': // 项目名称  
        this.setData({
          "projectName": form.selected.label,
          "projectId": form.selected.id,
        })
        break
      case 'selectLockStation': // 获取厂站  
        this.setData({
          "stationName": form.selected.label,
          "stationId": form.selected.id,
        })
        break
      case 'selectLockArea': // 区域名称  
        this.setData({
          "areaName": form.selected.label,
          "areaId": form.selected.id,
        });
        break
      case 'selectLockName': // 锁具名称  
        this.setData({
          "locakName": form.selected.label,
          "lockCode": form.selected.lockCode,
          "lockListId":form.selected.id
        })
        break
        
    }
    console.log(form) 
  },
  selectLockCompany(){
    Store.dispatch({
      type: 'SET_FROM',
      form: {
        key: 'selectLockCompany', // 后台要的更改字段 
        type: 1, // 表单类型 1： radio  2：select
      },
    })
    $.linkurl(`/pages/main/chooseForm/chooseForm`)
  },
  selectLockProject(){
    if (this.data.companyId == '') {
      $.toast('请先选择所属公司')
      return false
    }
    Store.dispatch({
      type: 'SET_FROM',
      form: {
        key: 'selectLockProject', // 后台要的更改字段 
        type: 1, // 表单类型 1： radio  2：select
        config: {
          parentId: this.data.companyId
        },
      },
    })
    $.linkurl(`/pages/main/chooseForm/chooseForm`)
  },
  selectLockStation (e) { // 厂站
    if (this.data.projectId == '') {
      $.toast('请先选择所属项目')
      return false
    } else {
      Store.dispatch({
        type: 'SET_FROM',
        form: {
          key: 'selectLockStation', // 后台要的更改字段 
          type: 1, // 表单类型 1： radio  2：select
          config: {
            parentId: this.data.projectId
          },
        },
      })
      $.linkurl(`/pages/main/chooseForm/chooseForm`)
      
    } 
  },
  selectLockArea() { // 区域
    if (this.data.stationId == '') {
      $.toast('请先选择所属厂站')
      return false
    }
    Store.dispatch({
      type: 'SET_FROM',
      form: {
        key: 'selectLockArea', // 后台要的更改字段 
        type:1, // 表单类型 1： radio  2：select
        config: {
          parentId: this.data.stationId
        },
      },
    })
    $.linkurl(`/pages/main/chooseForm/chooseForm`)
  },
  selectLockName(){
    if (this.data.areaId == '') {
      $.toast('请先选择所属区域')
      return false
    }
    Store.dispatch({
      type: 'SET_FROM',
      form: {
        key: 'selectLockName', // 后台要的更改字段 
        type:1, // 表单类型 1： radio  2：select
        config: {
          parentId: this.data.areaId
        },
      },
    })
    $.linkurl(`/pages/main/chooseForm/chooseForm`)
  },
  openTimeHandle(e) { // 打开时间弹框
    const {item}=$.detail(e)
    this.setData({timeType:item})
    if(item=='applyStartTime'){
      this.setData({
        isShowTime: true
      })
    }else if(item=='applyEndTime'&&this.data.applyStartTime!=''){
      this.setData({
        isShowTime2: true
      })
    }else{
      $.toast("请先选择开始时间")
    }
  },
  timeConfirmHandle(e) { // 时间组件传出来的数据
    var { time, key } = e.detail
    this.setData({
      [key]: time
    })
    console.log(time, key)
  },
  cancelMake(){
    $.linkurl('','back')
  },
  subTicket(){
    
    if(this.data.type=='apply'){
      if(this.data.lockCode==''||this.data.applyReason==''||this.data.applyStartTime==''||this.data.applyEndTime==''){
        $.toast('请输入完整的必填项信息')
        return false
      }
      api.$post(api.lockManage.applyLockOpen, { 
        lockCode: this.data.lockCode,
        applyReason: this.data.applyReason,
        applyStartTime: this.data.applyStartTime,
        applyEndTime: this.data.applyEndTime,
      }).then((res) => {
        if(res.code==200){
          $.toast('申请成功')
          $.linkurl(`/pages/lockMange/applyLock/applyLock`)
        }
      })
    }else{
      if(this.data.lockCode==''){
        $.toast('请输入完整的必填项信息')
        return false
      }
      api.$post(api.lockManage.lockAdd, { 
        lockCode: this.data.lockCode,
        deptId: this.data.lockListId,
        lockId: this.data.lockId,
      }).then((res) => {
        if(res.code==200){
          $.toast('绑定成功！')
          
        }
        $.linkurl(`/pages/lockMange/${this.data.urlName}/${this.data.urlName}`)
      })
    }
    
  },


  input_handle(e){
    this.setData({
      [e.currentTarget.dataset.key]: e.detail.value,
    })
  },
})